import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution501 {

    public static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    int last=-1;
    int count=0;
    int max=0;
    List<Integer> list=new ArrayList<>();

    public int[] findMode(TreeNode root) {
        dfs(root);
        int[] f=new int[list.size()];
        for (int i=0;i<list.size();i++){
            f[i]=list.get(i);
        }
        return f;
    }

    private void dfs(TreeNode root) {
        if (root.left!=null){
            dfs(root.left);
        }
        if (root.val==last){
            count++;
        }else{
            count=1;
            last=root.val;
        }
        if (count==max){
            list.add(root.val);
        }else if (count>max){
            list=new ArrayList<>();
            list.add(root.val);
            max=count;
        }
        if (root.right!=null){
            dfs(root.right);
        }
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new Solution501().findMode(new TreeNode(1, null, new TreeNode(2, new TreeNode(2), null)))));
    }
}
